//
//  WBNavigationController.swift
//  weibobo
//
//  Created by KIMI on 16/9/20.
//  Copyright © 2016年 KIMI. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    //重写pushViewController 监听控制器里面有几个子视图
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
//        print(viewControllers.count)
        
        if viewControllers.count > 0{
            
            if viewControllers.count == 1{
                
                let title = viewControllers.first?.title
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imgName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }else{
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imgName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
            viewController.title = "当前是第\(viewControllers.count + 1)页"
            
            //如果不是第一个控制器,隐藏tabBar
            viewController.hidesBottomBarWhenPushed = true
        }
        

        super.pushViewController(viewController, animated: animated)
    }
    
    

    func backAction() {
        
        popViewController(animated: true)
    }
    
    //边缘手势代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1{
            return false
        }
        return true
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
